const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:"",
    mindate:Math.round((new Date() - 365*86400*1000)),
    show:false,
    datastatus:0,//查看 0全部/1准备中/2进行中/3未完成 
    datafind:"",//搜索关键字
    show:false,
    findtime:"",//时间范围查找
    list: [],
    page: 1,//页码
    limit: 3,//一页数量
    id:0,//原材料id
    flag: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.id = options.id
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTabChange(event){
    if(event.detail.index === 1){
      this.data.datastatus = 1
    }else if(event.detail.index === 2){
      this.data.datastatus = 2
    }else if(event.detail.index === 0){
      this.data.datastatus = 0
    }else if(event.detail.index === 3){
      this.data.datastatus = 3
    }
    this.data.page = 1
    this.getList()
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
  },

  getList(search="",date="") {
    var that = this;
    var params = {
      page: that.data.page,//当前页码
      type: this.data.datastatus,//是否完成
      add_date:this.data.findtime,//时间范围
      id:this.data.id,//原材料id
      limit: that.data.limit,
      search:this.data.datafind,
    }
    //页面功能定义
    request.requestGet('material/get_stock_log', params, "加载中",function (res) {
      if (res.status === 200) {
        let list = that.data.list
        res.data.data.map((item) =>{
          return item.t = false;
        })
        if (that.data.page == 1) {
          list = []
        }
        list = [...list, ...res.data.data]
        if (list.length < res.data.count ){
          that.setData({
            page: that.data.page + 1,
            flag:true
          })
        } else {
          that.setData({
            flag:false
          })
        }
        that.setData({
          list: list,
          count: res.data.count
        })
      }
      
      }, function (res) {
        console.log(res,"请求失败")
      },
    )
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
      findtime : this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1])
    });
    this.getList("",this.data.date)
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  onSearch(e){
    this.setData({
      flag:true
    })
    this.data.page = 1
    this.data.datafind = e.detail
    this.getList(e.detail)
  },
})